package com.application.lucky.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.manager.ContactsManager;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.nav.FrontEndProcessor;
import com.application.lucky.utils.PhoneSystemUtil;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.LogUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.RequestCommonParams;
import com.qunar.im.ui.sdk.QIMSdk;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class AdActivity extends BaseActivity {

    private TextView tvCountDownTime;
    private ImageView ivAdImage;
    private Disposable mdDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        countdownTime();
        tvCountDownTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mdDisposable != null) {
                    mdDisposable.dispose();
                }
                jumpNextActivity();
            }
        });
        initFrontEndProcessor();

    }

    private void countdownTime() {
        if (UserManagers.getInstance().isLogin())
            ContactsManager.getInstance().loadFriends();


        mdDisposable = Flowable.intervalRange(0, 4, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {

                        tvCountDownTime.setText("点击跳过(" + (3 - aLong) + ")");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        jumpNextActivity();
                    }
                })
                .subscribe();

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }

    public void jumpNextActivity() {
        if (UserManagers.getInstance().isLogin()) {
//            if (UserManagers.getInstance().isLogin()) {
            UserManagers.getInstance().loadUserBean();
//            }
            startActivityAnim(new Intent(AdActivity.this, MainActivity.class));

        } else {
            startActivityAnim(new Intent(AdActivity.this, LoginActivity.class));
        }
        finish();
    }

    private void initFrontEndProcessor() {
        String imei = PhoneSystemUtil.getIMEI();
        if (RequestCommonParams.getInstance() != null) {
            if (!TextUtils.isEmpty(imei)) {
                RequestCommonParams.getInstance().updateImei(imei);
            } else {
                RequestCommonParams.getInstance().updateImei("0");
            }
        }
        new FrontEndProcessor().getConfig(new FrontEndProcessor.FrontEndProcessorCallback() {
            @Override
            public void success() {
                LogUtils.i("加载导航地址成功了");
            }
        });
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_ad;
    }

    private void initView() {
        tvCountDownTime = (TextView) findViewById(R.id.tv_count_down_time);
        ivAdImage = (ImageView) findViewById(R.id.iv_ad_image);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mdDisposable != null) {
            mdDisposable.dispose();
        }
    }

}
